Repository navigation
Conversation
path_from_row_pks() returns "" for a row whose only primary key value is
the empty string, and "" is a legitimate _next= cursor. Both the site
that emits the token and the site that reads it back tested it for
truthiness, so the token was discarded.
Emitting:
"next": next_value and str(next_value) or None
turns "" into None, while next_url is still built a few lines earlier
and stays non-null. docs/json_api.rst:99 says both are null on the final
page, so the response contradicted its own documentation. Reading back,
"if _next:" then ignored the cursor and the query returned page 1
again, so a client following next_url looped on the first row forever.
CREATE TABLE t (id TEXT PRIMARY KEY);
INSERT INTO t (id) VALUES ('a'), (''), ('c'), ('d'), 'e');
/d/t.json?_size=1 rows [''] next=None next_url=...&_next=
following it rows [''] next=None next_url=...&_next=
None is the no-cursor sentinel: it is initialised to None at
table.py:2508 and nothing assigns "" to it deliberately. The same
function already asks this correctly at table.py:2536
("if prefix is None: prefix = '\'").
A bare ?_next= with no value now means "rows after the empty primary
key" rather than being ignored, which is the only coherent reading of
an empty cursor. _next=a and _next=zzz are unchanged.
No docs change: json_api.rst already describes the fixed behaviour.
Test: tests/test_table_api.py::test_paginate_table_with_empty_string_primary_key
walks the pages and fails on cec5e6b with
assert '_next=None' in '...?_size=1&_next='
pytest -n auto -m 'not serial' 2782 passed, pytest -m serial 40 passed.
black, ruff, cog --check and codespell clean.
|
Hi @simonw — #2968 has been open since 28 September with the docs build green and no feedback yet, so this is the one nudge I planned. Recap: One thing I checked rather than assumed: Is there anything you'd like changed before this can go in? |
|
Hi @simonw — two small pagination fixes for Datasette have been green and conflict-free for a few weeks:
Both have regression tests, and the checks pass. Could you take a look when you have a moment? Happy to adjust anything. |
Muguett-DBY
left a comment
There was a problem hiding this comment.
Could the empty-cursor handling distinguish views from tables? Views use _next as an integer offset, so the new if _next is not None also sends an empty string into int(_next). With a view v AS SELECT id FROM items, GET /check/v.json?_size=1&_next= returns the first page on the base, but returns 500 (ValueError: invalid literal for int() with base 10: '') on this head. The same empty cursor on the table advances past the empty primary key as intended.
Perhaps a view could continue treating an empty offset as the first page, with a regression test for that route. I checked actual HTTP responses using the exact base and PR table_view_data functions, with the remaining Datasette code from b4c280c on Windows/Python 3.12; I have not built or run the full suite at this PR head.
The empty-string cursor change made `if _next is not None` apply to
views as well. Views paginate by integer offset, and their first page
arrives as `?_next=`, so `int("")` raised ValueError and returned 500.
An empty cursor now stays "no cursor" for views, while a table still
reads it as a real cursor for an empty primary key.
Refs simonw#2967, simonw#2968
Test: tests/test_table_api.py::test_view_ignores_an_empty_next_cursor
returns 500 before this change and passes after it. The same test
passes against origin/main's table.py, so this restores base behaviour.
tests/test_table_api.py: 129 passed, 8 xpassed; tests/test_html.py -k
view: 6 passed; black and ruff check clean.
|
Good catch — confirmed on the branch. Fixed in
Validation: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2968 +/- ##
=====================================
Coverage 0.00% 0.00%
=====================================
Files 79 79
Lines 13978 13980 +2
=====================================
- Misses 13978 13980 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Closes #2967, which has the longer write-up.
The defect in one place
path_from_row_pks()returns""for a row whose only primary key value is the empty string, and""is a legitimate_next=cursor. Both the site that emits the token and the site that reads it back test it for truthiness instead of identity:Noneis the no-cursor sentinel — initialised toNoneattable.py:2508, and nothing ever assigns""to it deliberately.The result
A client that follows
next_urlloops on the first page. With the fix it walks'',a,c,d,eonce each and the last page has both keysnull.Two things made this a contract violation rather than a curiosity:
docs/json_api.rst:99says"next"and"next_url"are bothnullon the final page. Herenextis null whilenext_urlis not.tests/test_table_api.py:333already asserts the two agree —urlencode({"_next": response.json()["next"]}) in path— and fails withassert '_next=None' in '...&_next='. It passes today only because every stock fixture has non-empty primary keys.The same function already asks this question correctly at
table.py:2536(if prefix is None: prefix = "$null"), androw_label_from_label_columnattable.py:287usesif value is None or value == "".Behaviour change worth naming
A bare
?_next=with no value now means "rows after the empty primary key" instead of being ignored and returning page 1. That is the only coherent reading of an empty cursor, but it is a change._next=aand_next=zzzbehave exactly as before.Testing
tests/test_table_api.py::test_paginate_table_with_empty_string_primary_keywalks the pages vianext_url, checksnext_urlandnextagree on every non-final page, asserts all five rows come back exactly once, and asserts the final page has both keysnull. It fails oncec5e6b2with the_next=None/_next=assertion above.pytest -n auto -m "not serial"— 2782 passed, 54 skipped, 6 xfailed, 15 xpassed, 143 subtestspytest -m serial— 40 passedblack --check .,ruff check datasette tests,cog --check docs/*.rst,codespell— cleanThe judgement call I left to you
This needs a table whose single text primary key holds
''. Legal SQLite, and nothing in Datasette rejects it, but I could not find a path there that does not involve importing a CSV with an empty key — so it may be rare enough that you would rather not carry the change. What convinced me it is a bug is the contradiction with the documented JSON contract, which holds whatever the input.No documentation change:
json_api.rstalready describes the fixed behaviour, the code was the thing that disagreed.